<template>
  <Panel>
    <template #title>
      <p>添加账号</p>
    </template>
    <template #content>
      <div>
        <el-form
          :model="ruleForm"
          status-icon
          :rules="rules"
          ref="ruleForm"
          label-width="100px"
          class="demo-ruleForm"
          size="mini"
        >
          <el-form-item label="用户名" prop="username">
            <el-input
              type="text"
              v-model="ruleForm.username"
              autocomplete="off"
            ></el-input>
          </el-form-item>
          <el-form-item label="密码" prop="pwd">
            <el-input
              :type="flag ? 'text' : 'password'"
              v-model="ruleForm.pwd"
              autocomplete="off"
            >
              <template #suffix>
                <i
                  class="iconfont"
                  :class="flag ? 'icon-eyes-' : 'icon-eyes'"
                  @click="flag = !flag"
                ></i>
              </template>
            </el-input>
          </el-form-item>
          <el-form-item label="用户组" prop="selectVal">
            <el-select v-model="ruleForm.selectVal" placeholder="请选择">
              <el-option
                v-for="item in usergroup"
                :key="item.value"
                :label="item.label"
                :value="item.value"
              >
              </el-option>
            </el-select>
          </el-form-item>
          <el-form-item>
            <el-button type="primary" @click="submitForm('ruleForm')"
              >提交</el-button
            >
            <el-button @click="resetForm('ruleForm')">重置</el-button>
          </el-form-item>
        </el-form>
      </div>
    </template>
  </Panel>
</template>

<script>
import { validateUserName, validatePwd } from '@/utils/validate'
import { addUser } from '@/api/user.js'

export default {
  data() {
    const select = (rule, value, callback) => {
      if (value === '') {
        callback(new Error('请选择用户角色'))
      } else {
        callback()
      }
    }
    return {
      flag: false,
      ruleForm: {
        username: '',
        pwd: '',
        selectVal: ''
      },
      rules: {
        username: [{ validator: validateUserName, trigger: 'blur' }],
        pwd: [{ validator: validatePwd, trigger: 'blur' }],
        selectVal: [{ validator: select, trigger: 'blur' }]
      },
      usergroup: [
        { value: '普通管理员', label: '普通管理员' },
        { value: '超级管理员', label: '超级管理员' }
      ]
    }
  },
  methods: {
    submitForm(formName) {
      this.$refs[formName].validate(async valid => {
        if (valid) {
          const data = await addUser({
            account: this.ruleForm.username,
            password: this.ruleForm.pwd,
            userGroup: this.ruleForm.selectVal
          })
          if (data.code === 0) {
            this.$router.push('/account/list')
          }
        } else {
          console.log('error submit!!')
          return false
        }
      })
    },
    resetForm(formName) {
      this.$refs[formName].resetFields()
    }
  }
}
</script>

<style lang="less" scoped>
/deep/.el-form {
  width: 50%;
  .el-input__suffix-inner {
    float: right;
  }
}
</style>
